﻿using System;

using Decal.Adapter;
using Decal.Adapter.Wrappers;
using Decal.Interop.Core;

/*
 * Created by Mag-nus. 8/19/2011
 * 
 * No license applied, feel free to use as you wish. H4CK TH3 PL4N3T? TR45H1NG 0UR R1GHT5? Y0U D3C1D3!
 * 
 * Notice how I use try/catch on every function that is called or raised by decal (by base events or user initiated events like buttons, etc...).
 * This is very important. Don't crash out your users!
 * 
 * In 2.9.6.4+ Host and Core both have Actions objects in them. They are essentially the same thing.
 * You sould use Host.Actions though so that your code compiles against 2.9.6.0 (even though I reference 2.9.6.5 in this project)
 * 
 * If you add this plugin to decal and then also create another plugin off of this sample, you will need to change the guid in
 * Properties/AssemblyInfo.cs to have both plugins in decal at the same time.
 * 
 * If you have issues compiling, remove the Decal.Adapater and VirindiViewService references and add the ones you have locally.
 * Decal.Adapter should be in C:\Games\Decal 3.0\
 * VirindiViewService should be in C:\Games\VirindiPlugins\VirindiViewService\
 * 
 * Started with SamplePlugin as a base, and expanded from there
 * Gastric from Solclaim
*/

namespace FellowShipHelper
{
    //Attaches events from core
    [WireUpBaseEvents]

    // FriendlyName is the name that will show up in the plugins list of the decal agent (the one in windows, not in-game)
    // View is the path to the xml file that contains info on how to draw our in-game plugin. The xml contains the name and icon our plugin shows in-game.
    // The view here is FellowShipHelper.mainView.xml because our projects default namespace is FellowShipHelper, and the file name is mainView.xml.
    // The other key here is that mainView.xml must be included as an embeded resource. If its not, your plugin will not show up in-game.
    [FriendlyName("FellowShipHelper"), View("FellowShipHelper.mainView.xml")]
    public class PluginCore : PluginBase
    {
        // New Checkboxes
        public static bool TapersCheckBox;
        public static bool ManaScarabsCheckBox;
        public static bool DarkScarabsCheckBox;
        public static bool DiamondScarabsCheckBox;
        public static bool PlatinumScarabsCheckBox;
        public static bool PyrealScarabsCheckBox;
        public static bool GoldScarabsCheckBox;
        public static bool SilverScarabsCheckBox;
        public static bool CopperScarabsCheckBox;
        public static bool IronScarabsCheckBox;
        public static bool LeadScarabsCheckBox;


        FellowShipChatManager FChatManager = null; // Our Fellowship Chatmanager class

        #region StartUp
        /// <summary>
        /// This is called when the plugin is started up. This happens only once.
        /// </summary>
        protected override void Startup()
        {
            try
            {
                // This initializes our static Globals class with references to the key objects your plugin will use, Host and Core.
                // The OOP way would be to pass Host and Core to your objects, but this is easier.
                Globals.Init("FellowShipHelper", Host, Core);
                
            }
            catch (Exception ex) { Util.LogError(ex); }
        }
        #endregion
        #region ShutDown
        /// <summary>
        /// This is called when the plugin is shut down. This happens only once.
        /// </summary>
        protected override void Shutdown()
        {
            try
            {
                // Dispose of our Classes
                if (FChatManager != null)
                {
                    FChatManager.Dispose();

                    Util.WriteToChat("Unloaded FellowShipHelper", eChatColors.BrightPeach);
                }
            }
            catch (Exception ex) { Util.LogError(ex); }
        }
        #endregion
        #region LogonComplete
        // This section runs after we have completly logged into game
        [BaseEvent("LoginComplete", "CharacterFilter")]
        private void CharacterFilter_LoginComplete(object sender, EventArgs e)
        {
            try
            {
                Util.WriteToChat("Plugin now online. Server population: " + Core.CharacterFilter.ServerPopulation, eChatColors.DarkBlue);
              
                // Try to init our plugin directory
                if (!Util.CheckPluginDirectory())
                {
                    // If we cant make our plugin directory, then shut us down.
                    this.Shutdown();
                    Util.WriteToChat("Unable to create plugin directory!", eChatColors.Red);
                }
                else
                {
                    // We are ok up to this point, lets create our FellowShipManager Class
                    try
                    {
                        // Our fchatmanager subscribes to our events we want, so just init it
                        FChatManager = new FellowShipChatManager();
                        Util.WriteToChat("FellowShipChatManager succesfully loaded.", eChatColors.BrightYellow);
                        string t = "My Main pack has 67 free slots, I am 87% burdened. Components i am tracking are 0 Mana Scarabs 0 Dark Scarabs 21 Diamond Scarabs 53 Platinum Scarabs 38 Pyreal Scarabs 20 Gold Scarabs 24 Silver Scarabs 30 Copper Scarabs 1575 Prismatic Tapers -f";
                        Util.WriteToChat("Total chat length is "+t.Length, eChatColors.BrightYellow);
                    }
                    catch (Exception ee)
                    {
                        Util.LogError(ee);
                        Util.WriteToChat(ee.Message, eChatColors.Red);
                    }
                }
            }
            catch (Exception ex) { Util.LogError(ex); }
        }
        #endregion
        #region LogOff
        [BaseEvent("Logoff", "CharacterFilter")]
        private void CharacterFilter_Logoff(object sender, Decal.Adapter.Wrappers.LogoffEventArgs e)
        {
            try
            {
                // Unsubscribe to events here, but know that this event is not gauranteed to happen. I've never seen it not fire though.
                // This is not the proper place to free up resources, but... its the easy way. It's not proper because of above statement.
                // Dispose of our Classes
                if (FChatManager != null)
                    FChatManager.Dispose();
            }
            catch (Exception ex) { Util.LogError(ex); }
        }
        #endregion

        #region UI Controls
        /*
         * Now we build our UI, we use this with our mainView.xml file
         * This is a entry for a checkbox, the name corresponds to our control
         * event, left and top is our position on our ui page witdh and height the size of our control
         * text is the name next to our control and because this is a checkbox, checked true or false
         * sets our default setting.
         * <control progid="DecalControls.CheckBox" name ="TestCheckBox" left="0" top="0" width="150" height="20" text="General Chat" checked="true" />
         * 
         * So to actually get input from our UI checkbox we need to declare a control event
         * which we can only do here, as we need to inherit the PluginBase to access these actions
         * 
         * TODO: I will be creating a xml file to store the user settings soon
         */
        /// <summary>
        /// Our Tapers CheckBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("TapersCheckBox", "Change")]
        void TapersCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                TapersCheckBox = true;
            }
            else if(!e.Checked)
            {
                TapersCheckBox = false;
            }
        }
        /// <summary>
        /// Our mana Scarabs checkbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("ManaScarabsCheckBox", "Change")]
        void ManaScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                ManaScarabsCheckBox = true;
            }
            else if(!e.Checked)
            {
                ManaScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Dark Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("DarkScarabsCheckBox", "Change")]
        void DarkScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                DarkScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                DarkScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Diamond Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("DiamondScarabsCheckBox", "Change")]
        void DiamondScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                DiamondScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                DiamondScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Our Platinum Scarabs Checkbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("PlatinumScarabsCheckBox", "Change")]
        void PlatinumScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                PlatinumScarabsCheckBox = true;
            }
            else if(!e.Checked)
            {
                PlatinumScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Pyreal Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("PyrealScarabsCheckBox", "Change")]
        void PyrealScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                PyrealScarabsCheckBox = true;
                
            }
            else if (!e.Checked)
            {
                PyrealScarabsCheckBox = false;
            }
        }
        /// <summary>
        ///  Gold Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("GoldScarabsCheckBox", "Change")]
        void GoldScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                GoldScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                GoldScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Silver Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("SilverScarabsCheckBox", "Change")]
        void SilverScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                SilverScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                SilverScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Copper Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("CopperScarabsCheckBox", "Change")]
        void CopperScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                CopperScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                CopperScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Iron Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("IronScarabsCheckBox", "Change")]
        void IronScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                IronScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                IronScarabsCheckBox = false;
            }
        }
        /// <summary>
        /// Lead Scarabs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [ControlEvent("LeadScarabsCheckBox", "Change")]
        void LeadScarabsCheckBox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            if (e.Checked)
            {
                LeadScarabsCheckBox = true;
            }
            else if (!e.Checked)
            {
                LeadScarabsCheckBox = false;
            }
        }
        #endregion
    }
}
